package com.acme.bankapp.service.bank;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.bank.Client;

/**
 * Service that performs operations under a bank, such as adding clients. 
 * @author Artem Nikitin
 *
 */
public class BankService {
	public void addClient(Bank bank, Client client) {
		if (client == null) {
			throw new IllegalArgumentException("Client cannot be null");
		}
		bank.addClient(client);
	}
	
	/**
	 * Returns client with specified name. If there is no clients with such name returns null
	 * @param name - name of client
	 * @return client with particular name or {@code null} if there is no clients with such name.
	 */
	public List<Client> getClientsByName(Bank bank, String name) {
		List<Client> result = new ArrayList<Client>();
		for (int i = 0; i < bank.getClientCount(); i++) {
			Client client = bank.getClient(i);
			if (client.getName().equals(name)) {
				result.add(client);
			}
		}
		return result;
	}
	
	public void printMaxAmountToWithdraw(Bank bank) {
		Iterator<Client> clients = bank.clients();
		while (clients.hasNext()) {
			Client client = clients.next();
			System.out.print(client.getClientSalutation() + ": ");
			System.out.println(client.getAccount().maxAmountToWithdraw());
		}
	}
}
